package euler.p051_100;

import euler.MainEuler;
import euler.helper.NaturalHelper;

public class Euler061 extends MainEuler {

    /*
        Triangle        P(3,n)=n*(n+1)/2       1, 3, 6, 10, 15, ...
        Square          P(4,n)=n^2             1, 4, 9, 16, 25, ...
        Pentagonal      P(5,n)=n*(3*n-1)/2      1, 5, 12, 22, 35, ...
        Hexagonal       P(6,n)=n*(2*n-1)        1, 6, 15, 28, 45, ...
        Heptagonal      P(7,n)=n*(5*n-3)/2      1, 7, 18, 34, 55, ...
        Octagonal       P(8,n)=n*(3*n-2)        1, 8, 21, 40, 65, ...

        The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.

        The set is cyclic, in that the last two digits of each number is
        the first two digits of the next number (including the last number with the first).
        Each polygonal type: triangle (P3,127=8128), square (P4,91=8281),
        and pentagonal (P5,44=2882), is represented by a different number in the set.
        This is the only set of 4-digit numbers with this property.

        Find the sum of the only ordered set of six cyclic 4-digit numbers
        for which each polygonal type: triangle, square, pentagonal, hexagonal,
        heptagonal, and octagonal, is represented by a different number in the set.
     */
    public String resolve() {

        for (int i = 19; i < 59; i++) {
            int n = i*(3*i-2);

            int[] arrayB = new int[8];
            int[] arrayI = new int[6];
            arrayI[0] = n;
            if (tryFill(arrayB, arrayI, n)) {
                int sum = 0;
                for (int j = 0; j < arrayI.length; j++) {
                    sum+=arrayI[j];
                }
                return String.valueOf(sum);
            }
        }

        return null;
    }

    private boolean tryFill(int[] arrayB, int[] arrayI, int n) {

        int mc = (n % 100) * 100;
        if (mc < 1000) {
            return false;
        }
        for (int i = 0; i < 100; i++) {
            int m = i + mc;
            int index = 0;

            if (arrayB[7] == 0 && NaturalHelper.isHeptagonal(m)) {
                index = 7;
            } else if (arrayB[6] == 0 && NaturalHelper.isHexagonal(m)) {
                index = 6;
            } else if (arrayB[5] == 0 && NaturalHelper.isPentagonal(m)) {
                index = 5;
            } else if (arrayB[4] == 0 && NaturalHelper.isSquare(m)) {
                index = 4;
            } else if (arrayB[3] == 0 && NaturalHelper.isTriangle(m)) {
                index = 3;
            }

            if (index != 0) {
                arrayB[index] = index;

                for (int j = 0; j < arrayI.length; j++) {
                    if (arrayI[j] == 0) {
                        arrayI[j] = m;
                        break;
                    }
                }

                boolean lleno = true;
                for (int j = 3; lleno && j < 8; j++) {
                    lleno = arrayB[j] != 0;
                }

                if (lleno && arrayI[0]/100 == arrayI[arrayI.length-1]%100) {
                    return true;
                } else if (tryFill(arrayB,arrayI,m)) {
                    return true;
                }

                for (int j = 0; j < arrayI.length; j++) {
                    if (arrayI[j] == m) {
                        arrayI[j] = 0;
                    }
                }
                arrayB[index] = 0;
            }
        }

        return false;
    }
}
